package com.freshcity.page.home
{
	import com.freshcity.common.ExperienceBar;
	import com.freshcity.common.HelpLink;
	import com.freshcity.common.NewMessage;
	import com.freshcity.common.SelfIcon;
	import com.freshcity.common.TaskIconWin;
	import com.freshcity.component.BaseGameObject;
	import com.freshcity.component.guide.GuideTipsManager;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.OpCodes;
	import com.freshcity.core.SoundTypeConst;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.lib.tooltip.ToolTipsManager;
	import com.freshcity.manager.CommunicatorManager;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.manager.support.MaskInfo;
	import com.freshcity.page.welcome.Register;
	import com.freshcity.page.zhitiao.ZhiTiaoBox;
	import com.freshcity.utils.MessageUtil;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.ImageLoader;
	import com.pblabs.debug.Logger;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;

	public class StatusPanel extends BaseGameObject
	{
		public var expAmt:int;
		public var energyAmt:int;
		public var levelAmt:int;
		public var timeBar:TimeBar;

		private var box:Sprite;
		private var upgradeWindow:UpgradeLevel;
		private var selfIcon:SelfIcon;
		private var energyBar:EnergyBar;
		private var voiceIcon:SoundPanel;
		private var zhiTiaoBox:TextField;
		private var newMsg:NewMessage;
		private var taskIcon:TaskIconWin;
		private var iTasks:Array;
		private var expBar:ExperienceBar;
		private var logIcon:TaskIconWin;
		private var userLog:String;
		private var msgAmt:int;

		public function StatusPanel(_name:String=null)
		{
			super(_name);
		}

		override public function initialize():void
		{
			super.initialize();

			box=new Sprite();
			LayerManager.sceneLayer.addChild(box);
			this.loadLogoImg();
			this.initExperienceBar();
			//this.initMsgIcon();
			this.initEnergyBar();
			this.initTimeBar();
			this.initHelpLink();
			this.initLogIcon();
			//this.initTaskIcon();
			//initZhiTiaoBoxLink();
			//initSoundBar();

			group.commu=this.owningGroup.getManager(CommunicatorManager) as CommunicatorManager;
			group.commu.register(OpCodes.GENERAL_ERROR, onShowError);
			group.commu.register(OpCodes.SYN_USER, onSynUser);
			group.commu.register(222, onSynFlower);
			group.commu.register(OpCodes.SYN_EXPERIENCE, onSynExperience);
			group.commu.register(OpCodes.SYN_REWARD, onSynReward);
			group.commu.register(OpCodes.AVAILABLE_UPGREADE, onAvailableUpgrade);
			group.commu.register(OpCodes.SYN_ENERGY, onSynEnergy);
			group.commu.register(OpCodes.UPGRADE_LEVEL, onUpgradeLevel);
			
			group.commu.send(OpCodes.SEND_LOGIN_EXP, {});
		}
		
		private function loadLogoImg():void
		{
			var loader:ImageLoader=new ImageLoader(GlobalVar.MAIN_PATH 
				+ "/img/" 
				+ GlobalVar.LOGOBAR_IMAGE_ID + "?v=" 
				+ GlobalVar.version, {y:50, onComplete: loadCompleteHandler});
			loader.load();
		}

		private function onShowError(data:*):void
		{
			group.msg.showRollTipsMsg(data);
		}
		
		private function loadCompleteHandler(event:LoaderEvent):void
		{
			var loc1:DisplayObject=event.target.content;
			loc1.x = 17;
			loc1.y = 14;
			box.addChild(loc1);
		}

		private function onSynUser(data:*):void
		{
			group.user=data;
			GlobalVar.LOGIN_USER=data;
		}
		
		private function onSynFlower(data:*):void
		{
			var s:String = "获得玫瑰"+data+"玫";
			group.msg.showRollTipsMsg(s);
			group.msg.showFixRollMsg(s);
			group.user.flowerAmt += data;
			Logger.debug(this,"onSynFlower","user flower amt is:" + GlobalVar.LOGIN_USER.flowerAmt);
		}

		private function initZhiTiaoBoxLink():void
		{
			zhiTiaoBox=StringUtil.addHyLink("纸条箱", "", openZhiTiaoBox);
			zhiTiaoBox.x=680;
			zhiTiaoBox.y=-15;
			box.addChild(zhiTiaoBox);
		}

		private function openZhiTiaoBox(e:Event):void
		{
			group.commu.send(OpCodes.QUERY_DATE_MSG, {}, onLoadZhiTiao);
		}

		private function onLoadZhiTiao(data:*):void
		{
			group.commu.unRegister(OpCodes.QUERY_DATE_MSG);

			var ztBox:ZhiTiaoBox=new ZhiTiaoBox();
			ztBox.owningGroup=group;
			ztBox.initialize();

			ztBox.show(data);
		}

		private function initExperienceBar():void
		{
			expBar=new ExperienceBar();
			expBar.x=590;
			expBar.y=25;
			expBar.toolTipData="信用级别";
			ToolTipsManager.register(expBar);
			expBar.levelValue.text=MessageUtil.decodeLevel(group.user.level);
			box.addChild(expBar);

			selfIcon=new SelfIcon();
			selfIcon.visible=false;
			selfIcon.mouseChildren=false;
			selfIcon.buttonMode=true;
			selfIcon.x=10;
			selfIcon.y=521;
			box.addChild(selfIcon);
			selfIcon.addEventListener(MouseEvent.CLICK, showUpgrade);

			energyAmt=group.user.energy;
			expAmt=int(group.user.exp) + int(group.user.dailyExp);
			//showUserLevel();

			//experienceBar.levelValue.text = String(levelAmt);
			//experienceBar.expValue.text = expAmt+"/"+group.user.needExp;
			//experienceBar.expRender.width = (expAmt/group.user.needExp>1?1:expAmt/group.user.needExp)*104;
		}

		private function showUserLevel():void
		{
			this.levelAmt=group.user.level;
			expBar.levelValue.text=MessageUtil.decodeLevel(group.user.level);
		}
		
		private function initLogIcon():void
		{
			logIcon=new TaskIconWin();
			logIcon.mouseChildren=false;
			logIcon.buttonMode=true;
			logIcon.x=520;
			logIcon.y=-6;
			box.addChild(logIcon);
			
			logIcon.toolTipData="动态";
			ToolTipsManager.register(logIcon);
			var result:* = MessageUtil.decodeUserLog(this.group.user);
			this.userLog = result.log;
			this.msgAmt = result.amt;
			this.logIcon.label.text=String(this.msgAmt);
			
			this.logIcon.addEventListener(MouseEvent.CLICK,onShowUserLog);
			this.group.commu.register(OpCodes.SYN_USER_LOG,synUserLogCallback);
			
			if(this.msgAmt>0){
				TweenLite.delayedCall(0.8,onShowUserLog);
			}
		}		
		
		private function synUserLogCallback(data:*):void
		{
			if(data is String){
				this.msgAmt++;
				this.group.user.userLog = data;
				var result:* = MessageUtil.decodeUserLog(this.group.user);
				this.userLog = result.log;
				this.logIcon.label.text=String(this.msgAmt);
			}
		}
		
		private function onShowUserLog(event:Event=null):void
		{
			var log:UserLog = new UserLog();
			log.main = this.group;
			log.logData = this.userLog;
			log.show();
			this.msgAmt = 0;
			this.logIcon.label.text=String(this.msgAmt);
		}

		private function initTaskIcon():void
		{
			taskIcon=new TaskIconWin();
			taskIcon.buttonMode=true;
			taskIcon.visible=false;
			taskIcon.x=700;
			taskIcon.y=10;
			box.addChild(taskIcon);

			taskIcon.toolTipData="交互任务";
			ToolTipsManager.register(taskIcon);
			this.group.commu.send(OpCodes.SYN_ITASK, {}, synITaskCallback);
		}

		private function synITaskCallback(data:*):void
		{
			if (data is Array)
			{
				var len:int=data.length;
				this.iTasks=data;
				if (len > 0)
				{
					this.taskIcon.label.text=String(len);
					this.taskIcon.visible=true;
					taskIcon.addEventListener(MouseEvent.CLICK, onShowTaskMsg);
				}
				else
				{
					this.taskIcon.visible=false;
					taskIcon.removeEventListener(MouseEvent.CLICK, onShowTaskMsg);
				}
			}
		}

		protected function onShowTaskMsg(event:MouseEvent):void
		{
			var icon:TaskIcon=new TaskIcon(this.iTasks);
			icon.main=this.group;
			icon.show();
		}

		private function initMsgIcon():void
		{
			newMsg=new NewMessage();
			newMsg.visible=false;
			newMsg.x=700;
			newMsg.y=10;
			box.addChild(newMsg);

			newMsg.toolTipData="新留言";
			ToolTipsManager.register(newMsg);

			//newMsg.addEventListener(MouseEvent.CLICK, onShowMessage);
		}

		private function initHelpLink():void
		{
			var help:HelpLink=new HelpLink();
			help.buttonMode=true;
			help.x=744;
			help.y=25;
			box.addChild(help);
			help.addEventListener(MouseEvent.CLICK, onHelpShow);
		}

		protected function onHelpShow(event:MouseEvent):void
		{
			if (!group.helpHome)
				group.initHelp();
			group.helpHome.show();
		}

		private function initEnergyBar():void
		{
			energyBar=new EnergyBar(box, 648, 25);
			energyBar.toolTipData="今日体力值";
			energyBar.data=energyAmt;
		}

		private function initTimeBar():void
		{
			timeBar=new TimeBar(LayerManager.msgTipLayer);
			timeBar.visible=false;
		}

		private function initSoundBar():void
		{
			voiceIcon=new SoundPanel(box, 740, -15);
			voiceIcon.addEventListener(MouseEvent.CLICK, onPlaySound);
		}

		protected function onPlaySound(event:MouseEvent):void
		{
			group.soundManager.playSound(SoundTypeConst.ComeIn, true);
		}

		private function onUpgradeLevel(data:*):void
		{
			if (data.status == "true")
			{
				GlobalVar.LOGIN_USER=group.user=data.user;
				showUserLevel();
				expAmt=int(group.user.exp) + int(group.user.dailyExp);
				showExpBar();
				if (upgradeWindow && upgradeWindow.parent)
				{
					LayerManager.alertLayer.removeChild(upgradeWindow);
					EffectManager.hideUIMask();
				}

				if(data.user.level==2){
					showTipWin();
				}else if(data.user.level>2){
					var levelStr:String = StringUtil.addColor(data.user.level, "#ff0000");
					var s2:String = "个人信用等级提升到".concat("",levelStr,"级<br/>");
					if(data.user.level<6) s2 += "".concat("<p align='right'>&nbsp;","<a href='event:'>",">>继续提升信用","</a></p>");
					group.msg.showPopUpMsg(s2,MsgManager.COOL,0,MsgManager.BOTTOM_RIGHT,showUpgrade);
					group.msg.showFixRollMsg("个人信用等级提升到" + levelStr + "级");
				}
				Dispatcher.dispatchEvent(new DataEvent(EventName.LevelUp,data.user));
			}
		}
		
		private function showTipWin():void
		{
			var s:String = "非常棒！你已经完成了基础的工作。现在,你将开始寻找TA的旅程。祝你好运！";
			EffectManager.addUIMask([],0);
			group.guideTip.getGuideTips(s,finishGuide);
		}		
		
		private function finishGuide():void
		{
			EffectManager.hideUIMask();
			group.guideTip.hideGuideTips();
			var s:String = "点击这里发现新人";
			var position:Point = new Point(340,520);
			var size:Point = new Point(40,50);
			
			var loc1:MaskInfo=new MaskInfo(position,size);
			//EffectManager.addUIMask([loc1],0);
			GuideTipsManager.getLineBoxTips(s,loc1,null,2000,GuideTipsManager.Dir_BL);			
		}		

		private function onAvailableUpgrade(data:*):void
		{
			var level:int=group.user.level;
			showExpBar();
		}

		public function showUpgrade(event:Event=null):void
		{
			if (group.user.level <=1 ){
				GuideTipsManager.hideLineBoxTips();
			}
			if (group.user.level == 0)
			{
				var reg:Register=new Register();
				reg.main=group;
				reg.show();
			}else
			{
				upgradeWindow=new UpgradeLevel(group);
				upgradeWindow.x=(group.stage.stageWidth) / 2;
				upgradeWindow.y=(group.stage.stageHeight) / 2;
				LayerManager.alertLayer.addChild(upgradeWindow);
				TweenLite.from(upgradeWindow, 1, {y: "+300", ease: Back.easeOut});
			}
		}

		private function onSynEnergy(data:*):void
		{
			GlobalVar.LOGIN_USER.energy=group.user.energy=data;
			energyBar.data=data;
		}

		private function onSynExperience(data:*):void
		{
			var e:int=int(data);
			group.msg.showRollTipsMsg("经验" + " + " + e);
			group.user.dailyExp=int(group.user.dailyExp) + e;
			GlobalVar.LOGIN_USER=group.user;
			expAmt+=e;
			showExpBar();
		}

		private function onSynReward(data:*):void
		{
			var type:String=data.type;
			var amt:int=int(data.amt);
			if (type == 'hongdou')
			{
				if (amt >= 0)
				{
					group.msg.showRollTipsMsg("玫瑰" + " + " + amt);
				}
				else
				{
					group.msg.showRollTipsMsg("玫瑰" + " - " + Math.abs(amt));
				}
			}
		}

		private function showExpBar():void
		{
			var needExp:int=group.user.needExp;
			var ui:int = group.user.income;
			var nui:int = group.user.needIncome;
			var level:int = group.user.level;
			
			selfIcon.visible = (expAmt>=needExp)&& (ui>=nui) && (level <= 5);
			
			//TweenLite.to(experienceBar.expRender,0.5,{onComplete:onUpdateExpValue,width:w,delay:0.5,onCompleteParams:[expAmt,needExp]});
		}

		private function onUpdateExpValue(newExp:int, needExp:int):void
		{
			//experienceBar.expValue.text = newExp+"/"+needExp;
		}
	}
}